package com.windrift.springhibernate.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: cy
 * Date: 6/05/12
 * Time: 2:30 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class FileUploadController {

    @RequestMapping(value="/fileupload", method = RequestMethod.GET)
    public String showFileUploadPage() {
        return "fileupload";
    }
    
    @RequestMapping(value = "/fileupload", method = RequestMethod.POST)
    public String handleFormUpload(@RequestParam("name") String name,
                                   @RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
                // store the bytes somewhere
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            System.out.println("uploadSuccess");
            return "redirect:uploadSuccess";
        } else {
            System.out.println("uploadFailure");
            return "redirect:uploadFailure";
        }
    }
}
